package com.jwthhealth_pub.old.common.Address.ormlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.AndroidConnectionSource;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.support.ConnectionSource;
import com.jwthhealth_pub.app.App;
import com.jwthhealth_pub.old.common.Address.Address;
import com.jwthhealth_pub.old.signutil.db.SQLdm;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2017/5/5.
 */

public class OrmManager {
    Context mContext;
    private Dao<Address, String> addressesDao;

    public Dao<Address, String> getAddressesDao() {
        return addressesDao;
    }

    public OrmManager(Context _context) throws SQLException {
        this.mContext = _context;

        try {
            SQLdm sqLdm = new SQLdm();
            SQLiteDatabase sqLiteDatabase = sqLdm.openDatabase(App.mContext, SQLdm.FILEADDRESS);
            ConnectionSource androidConnectionSource = new AndroidConnectionSource(
                    sqLiteDatabase);

            addressesDao = DaoManager.createDao(androidConnectionSource, Address.class);
            List<Address> addresses = addressesDao.queryForAll();
            Log.d("OrmManager", "size:" + addresses.size());
            for (int i=0; i<addresses.size(); i++) {
                Log.d("OrmManager", addresses.get(i).getName());
            }


        } catch (Exception e) {
            Log.d("OrmManager", e.toString());
        }
    }
}
